/**
 * 角色常量定义
 * 统一管理前端角色相关的字符串常量，避免硬编码
 * 符合《智慧社区微服务架构总览.md》权威标准
 * 
 * @author Wu.Liang
 * @date 2025-01-30
 * @updated 2025-01-30 - 精简常量声明，避免重复定义
 */

// ==================== 角色代码常量 ====================

/**
 * 角色代码常量 - 统一角色编码定义
 */
export const ROLE_CODES = {
  // 管理员角色
  SUPER_ADMIN: 'SUPER_ADMIN',
  ADMIN: 'ADMIN',
  
  // 区域管理角色
  REGION_ADMIN: 'REGION_ADMIN',
  COMMUNITY_ADMIN: 'COMMUNITY_ADMIN',
  
  // 物业管理角色
  PROPERTY_ADMIN: 'PROPERTY_ADMIN',
  PROPERTY_STAFF: 'PROPERTY_STAFF',
  
  // 安防管理角色
  SECURITY_ADMIN: 'SECURITY_ADMIN',
  SECURITY_STAFF: 'SECURITY_STAFF',
  
  // 保洁管理角色
  CLEANING_ADMIN: 'CLEANING_ADMIN',
  CLEANING_STAFF: 'CLEANING_STAFF',
  
  // 停车管理角色
  PARKING_ADMIN: 'PARKING_ADMIN',
  PARKING_STAFF: 'PARKING_STAFF',
  
  // 维修管理角色
  REPAIR_STAFF: 'REPAIR_STAFF',
  
  // 楼栋管理角色
  BUILDING_ADMIN: 'BUILDING_ADMIN',
  UNIT_ADMIN: 'UNIT_ADMIN',
  HOUSEHOLD_ADMIN: 'HOUSEHOLD_ADMIN',
  
  // 居民角色
  RESIDENT: 'RESIDENT',
  OWNER: 'OWNER',
  NORMAL_USER: 'NORMAL_USER'
} as const

// ==================== 角色名称常量 ====================

/**
 * 角色名称常量 - 统一角色显示名称
 */
export const ROLE_NAMES = {
  SUPER_ADMIN: '超级管理员',
  ADMIN: '管理员',
  REGION_ADMIN: '区域管理员',
  COMMUNITY_ADMIN: '社区管理员',
  PROPERTY_ADMIN: '物业管理员',
  PROPERTY_STAFF: '物业人员',
  SECURITY_ADMIN: '安保管理',
  SECURITY_STAFF: '安保人员',
  CLEANING_ADMIN: '保洁管理',
  CLEANING_STAFF: '保洁人员',
  PARKING_ADMIN: '停车管理',
  PARKING_STAFF: '停车人员',
  REPAIR_STAFF: '维修人员',
  BUILDING_ADMIN: '楼栋管理员',
  UNIT_ADMIN: '单元管理员',
  HOUSEHOLD_ADMIN: '房户管理员',
  RESIDENT: '居民',
  OWNER: '业主',
  NORMAL_USER: '普通用户',
  UNKNOWN: '未知角色'
} as const

// ==================== 权限前缀常量 ====================

/**
 * 权限前缀常量 - 统一权限前缀定义
 */
export const PERMISSION_PREFIXES = {
  SUPER_ADMIN: 'super:admin:',
  ADMIN: 'system:admin:',
  REGION_ADMIN: 'region:admin:',
  COMMUNITY_ADMIN: 'community:admin:',
  PROPERTY_ADMIN: 'property:admin:',
  PROPERTY_STAFF: 'property:staff:',
  SECURITY_ADMIN: 'security:admin:',
  SECURITY_STAFF: 'security:staff:',
  CLEANING_ADMIN: 'cleaning:admin:',
  CLEANING_STAFF: 'cleaning:staff:',
  PARKING_ADMIN: 'parking:admin:',
  PARKING_STAFF: 'parking:staff:',
  REPAIR_STAFF: 'repair:staff:',
  BUILDING_ADMIN: 'building:admin:',
  UNIT_ADMIN: 'unit:admin:',
  HOUSEHOLD_ADMIN: 'household:admin:',
  RESIDENT: 'resident:',
  OWNER: 'owner:',
  NORMAL_USER: 'user:'
} as const

// ==================== 角色组常量 ====================

/**
 * 角色组常量 - 统一角色分组定义
 */
export const ROLE_GROUPS = {
  // 管理员角色组
  ADMIN_ROLES: [ROLE_CODES.SUPER_ADMIN, ROLE_CODES.ADMIN],
  
  // 区域管理角色组
  REGION_MANAGEMENT_ROLES: [ROLE_CODES.REGION_ADMIN, ROLE_CODES.COMMUNITY_ADMIN],
  
  // 物业管理角色组
  PROPERTY_MANAGEMENT_ROLES: [ROLE_CODES.PROPERTY_ADMIN, ROLE_CODES.PROPERTY_STAFF],
  
  // 安防管理角色组
  SECURITY_MANAGEMENT_ROLES: [ROLE_CODES.SECURITY_ADMIN, ROLE_CODES.SECURITY_STAFF],
  
  // 保洁管理角色组
  CLEANING_MANAGEMENT_ROLES: [ROLE_CODES.CLEANING_ADMIN, ROLE_CODES.CLEANING_STAFF],
  
  // 停车管理角色组
  PARKING_MANAGEMENT_ROLES: [ROLE_CODES.PARKING_ADMIN, ROLE_CODES.PARKING_STAFF],
  
  // 楼栋管理角色组
  BUILDING_MANAGEMENT_ROLES: [ROLE_CODES.BUILDING_ADMIN, ROLE_CODES.UNIT_ADMIN, ROLE_CODES.HOUSEHOLD_ADMIN],
  
  // 居民角色组
  RESIDENT_ROLES: [ROLE_CODES.RESIDENT, ROLE_CODES.OWNER, ROLE_CODES.NORMAL_USER],
  
  // 所有管理角色组
  ALL_ADMIN_ROLES: [
    ROLE_CODES.SUPER_ADMIN,
    ROLE_CODES.ADMIN,
    ROLE_CODES.REGION_ADMIN,
    ROLE_CODES.COMMUNITY_ADMIN,
    ROLE_CODES.PROPERTY_ADMIN,
    ROLE_CODES.BUILDING_ADMIN,
    ROLE_CODES.UNIT_ADMIN,
    ROLE_CODES.HOUSEHOLD_ADMIN,
    ROLE_CODES.SECURITY_ADMIN,
    ROLE_CODES.CLEANING_ADMIN,
    ROLE_CODES.PARKING_ADMIN
  ],
  
  // 所有工作人员角色组
  ALL_STAFF_ROLES: [
    ROLE_CODES.PROPERTY_STAFF,
    ROLE_CODES.SECURITY_STAFF,
    ROLE_CODES.CLEANING_STAFF,
    ROLE_CODES.PARKING_STAFF,
    ROLE_CODES.REPAIR_STAFF
  ]
} as const

// ==================== 工具函数 ====================

/**
 * 检查是否为管理员角色
 * @param roleCode 角色代码
 * @returns 是否为管理员角色
 */
export function isAdminRole(roleCode: string): boolean {
  if (!roleCode) {
    return false
  }
  return ROLE_CODES.SUPER_ADMIN === roleCode || ROLE_CODES.ADMIN === roleCode
}

/**
 * 检查是否为超级管理员角色
 * @param roleCode 角色代码
 * @returns 是否为超级管理员角色
 */
export function isSuperAdminRole(roleCode: string): boolean {
  return ROLE_CODES.SUPER_ADMIN === roleCode
}

/**
 * 检查是否为区域管理角色
 * @param roleCode 角色代码
 * @returns 是否为区域管理角色
 */
export function isRegionManagementRole(roleCode: string): boolean {
  if (!roleCode) {
    return false
  }
  return ROLE_GROUPS.REGION_MANAGEMENT_ROLES.includes(roleCode as any)
}

/**
 * 检查是否为物业管理角色
 * @param roleCode 角色代码
 * @returns 是否为物业管理角色
 */
export function isPropertyManagementRole(roleCode: string): boolean {
  if (!roleCode) {
    return false
  }
  return ROLE_GROUPS.PROPERTY_MANAGEMENT_ROLES.includes(roleCode as any)
}

/**
 * 检查是否为安防管理角色
 * @param roleCode 角色代码
 * @returns 是否为安防管理角色
 */
export function isSecurityManagementRole(roleCode: string): boolean {
  if (!roleCode) {
    return false
  }
  return ROLE_GROUPS.SECURITY_MANAGEMENT_ROLES.includes(roleCode as any)
}

/**
 * 检查是否为管理角色
 * @param roleCode 角色代码
 * @returns 是否为管理角色
 */
export function isManagementRole(roleCode: string): boolean {
  if (!roleCode) {
    return false
  }
  return ROLE_GROUPS.ALL_ADMIN_ROLES.includes(roleCode as any)
}

/**
 * 获取角色名称
 * @param roleCode 角色代码
 * @returns 角色名称
 */
export function getRoleName(roleCode: string): string {
  if (!roleCode) {
    return ROLE_NAMES.UNKNOWN
  }
  
  switch (roleCode) {
    case ROLE_CODES.SUPER_ADMIN:
      return ROLE_NAMES.SUPER_ADMIN
    case ROLE_CODES.ADMIN:
      return ROLE_NAMES.ADMIN
    case ROLE_CODES.REGION_ADMIN:
      return ROLE_NAMES.REGION_ADMIN
    case ROLE_CODES.COMMUNITY_ADMIN:
      return ROLE_NAMES.COMMUNITY_ADMIN
    case ROLE_CODES.PROPERTY_ADMIN:
      return ROLE_NAMES.PROPERTY_ADMIN
    case ROLE_CODES.PROPERTY_STAFF:
      return ROLE_NAMES.PROPERTY_STAFF
    case ROLE_CODES.SECURITY_ADMIN:
      return ROLE_NAMES.SECURITY_ADMIN
    case ROLE_CODES.SECURITY_STAFF:
      return ROLE_NAMES.SECURITY_STAFF
    case ROLE_CODES.CLEANING_ADMIN:
      return ROLE_NAMES.CLEANING_ADMIN
    case ROLE_CODES.CLEANING_STAFF:
      return ROLE_NAMES.CLEANING_STAFF
    case ROLE_CODES.PARKING_ADMIN:
      return ROLE_NAMES.PARKING_ADMIN
    case ROLE_CODES.PARKING_STAFF:
      return ROLE_NAMES.PARKING_STAFF
    case ROLE_CODES.REPAIR_STAFF:
      return ROLE_NAMES.REPAIR_STAFF
    case ROLE_CODES.BUILDING_ADMIN:
      return ROLE_NAMES.BUILDING_ADMIN
    case ROLE_CODES.UNIT_ADMIN:
      return ROLE_NAMES.UNIT_ADMIN
    case ROLE_CODES.HOUSEHOLD_ADMIN:
      return ROLE_NAMES.HOUSEHOLD_ADMIN
    case ROLE_CODES.RESIDENT:
      return ROLE_NAMES.RESIDENT
    case ROLE_CODES.OWNER:
      return ROLE_NAMES.OWNER
    case ROLE_CODES.NORMAL_USER:
      return ROLE_NAMES.NORMAL_USER
    default:
      return ROLE_NAMES.UNKNOWN
  }
}

/**
 * 检查角色是否在指定角色组中
 * @param roleCode 角色代码
 * @param roleGroup 角色组
 * @returns 是否在角色组中
 */
export function isRoleInGroup(roleCode: string, roleGroup: readonly string[]): boolean {
  if (!roleCode || !roleGroup) {
    return false
  }
  return roleGroup.includes(roleCode)
}

/**
 * 获取角色权限前缀
 * @param roleCode 角色代码
 * @returns 权限前缀
 */
export function getRolePermissionPrefix(roleCode: string): string {
  if (!roleCode) {
    return PERMISSION_PREFIXES.NORMAL_USER
  }
  
  switch (roleCode) {
    case ROLE_CODES.SUPER_ADMIN:
      return PERMISSION_PREFIXES.SUPER_ADMIN
    case ROLE_CODES.ADMIN:
      return PERMISSION_PREFIXES.ADMIN
    case ROLE_CODES.REGION_ADMIN:
      return PERMISSION_PREFIXES.REGION_ADMIN
    case ROLE_CODES.COMMUNITY_ADMIN:
      return PERMISSION_PREFIXES.COMMUNITY_ADMIN
    case ROLE_CODES.PROPERTY_ADMIN:
      return PERMISSION_PREFIXES.PROPERTY_ADMIN
    case ROLE_CODES.PROPERTY_STAFF:
      return PERMISSION_PREFIXES.PROPERTY_STAFF
    case ROLE_CODES.SECURITY_ADMIN:
      return PERMISSION_PREFIXES.SECURITY_ADMIN
    case ROLE_CODES.SECURITY_STAFF:
      return PERMISSION_PREFIXES.SECURITY_STAFF
    case ROLE_CODES.CLEANING_ADMIN:
      return PERMISSION_PREFIXES.CLEANING_ADMIN
    case ROLE_CODES.CLEANING_STAFF:
      return PERMISSION_PREFIXES.CLEANING_STAFF
    case ROLE_CODES.PARKING_ADMIN:
      return PERMISSION_PREFIXES.PARKING_ADMIN
    case ROLE_CODES.PARKING_STAFF:
      return PERMISSION_PREFIXES.PARKING_STAFF
    case ROLE_CODES.REPAIR_STAFF:
      return PERMISSION_PREFIXES.REPAIR_STAFF
    case ROLE_CODES.BUILDING_ADMIN:
      return PERMISSION_PREFIXES.BUILDING_ADMIN
    case ROLE_CODES.UNIT_ADMIN:
      return PERMISSION_PREFIXES.UNIT_ADMIN
    case ROLE_CODES.HOUSEHOLD_ADMIN:
      return PERMISSION_PREFIXES.HOUSEHOLD_ADMIN
    case ROLE_CODES.RESIDENT:
      return PERMISSION_PREFIXES.RESIDENT
    case ROLE_CODES.OWNER:
      return PERMISSION_PREFIXES.OWNER
    case ROLE_CODES.NORMAL_USER:
      return PERMISSION_PREFIXES.NORMAL_USER
    default:
      return PERMISSION_PREFIXES.NORMAL_USER
  }
} 